package com.yx.louti.login.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.yx.louti.authCode.service.AuthCodeService;
import com.yx.louti.globalException.model.BizAssert;
import com.yx.louti.login.enums.LoginType;
import com.yx.louti.login.model.LoginUserModel;
import com.yx.louti.login.service.LoginUserService;
import com.yx.louti.web.bean.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class LoginUserController {
    private static final Log log = LogFactory.get();

    @Autowired
    AuthCodeService authCodeService;
    @Autowired
    LoginUserService loginUserService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public User login(@RequestBody LoginUserModel loginUserModel) {
        BizAssert.notNull(loginUserModel,"登陆表单不能为空!");
        BizAssert.notNull(loginUserModel.getLoginType(),"登陆类型不能为空!");
        //是否通过校验
        boolean verifyAuthCode;
        if(LoginType.密码登录 == loginUserModel.getLoginType() ){
            //先校图像验证码
            verifyAuthCode = authCodeService.verifyAuthCode(loginUserModel.getAuthCodeId(), loginUserModel.getAuthCode());
            BizAssert.isTrue(verifyAuthCode, "验证码错误");
            //再校验账号密码
            return loginUserService.checkAccountAndPassWord(loginUserModel);
        }else if (LoginType.手机验证码登录 == loginUserModel.getLoginType()){
            //手机验证码登陆 先不做
            BizAssert.isFalse(true,"手机验证码登录作者还没写!");
        }else {
            BizAssert.isFalse(true,"未实现的登陆方式!");
        }
        return null;
    }


}
